package cn.xfdzx.android.apps.shop.apps;

import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatDelegate;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.DefaultRefreshInitializer;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.tencent.bugly.crashreport.CrashReport;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tencent.rtmp.TXLiveBase;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;

import java.util.List;

import cn.jpush.android.api.JPushInterface;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.util.DynamicTimeFormat;
import cn.xfdzx.android.apps.shop.util.OneLoginUtils;
import cn.xfdzx.android.apps.shop.util.UtilsData;

import static cn.xfdzx.android.apps.shop.apps.Constants.APP_TOKEN;

public class MyApp extends Application {

    public static MyApp app;
    public static IWXAPI mWxApi;

    String licenceURL = "http://license.vod2.myqcloud.com/license/v1/449571e1520ae3cefdc0a50fef9b161b/TXLiveSDK.licence"; // 获取到的 licence url
    String licenceKey = "22c6c7c867c882bc59f2028559e7e8ce"; // 获取到的 licence key
    static {
        //启用矢量图兼容
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
        //设置全局默认配置（优先级最低，会被其他设置覆盖）
        SmartRefreshLayout.setDefaultRefreshInitializer(new DefaultRefreshInitializer() {
            @Override
            public void initialize(@NonNull Context context, @NonNull RefreshLayout layout) {
                //全局设置（优先级最低）
                layout.setEnableAutoLoadMore(true);
                layout.setEnableOverScrollDrag(false);
                layout.setEnableOverScrollBounce(true);
                layout.setEnableLoadMoreWhenContentNotFull(true);
                layout.setEnableScrollContentWhenRefreshed(true);
                layout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);
                layout.getLayout().setTag("close egg");
            }
        });
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @NonNull
            @Override
            public RefreshHeader createRefreshHeader(@NonNull Context context, @NonNull RefreshLayout layout) {
                //全局设置主题颜色（优先级第二低，可以覆盖 DefaultRefreshInitializer 的配置，与下面的ClassicsHeader绑定）
                return new ClassicsHeader(context).setTimeFormat(new DynamicTimeFormat("更新于 %s"));
            }
        });
    }

    public static MyApp getInstance() {

        return app;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
        /**
         * OneLogin 初始化
         */
        OneLoginUtils.init(this);
        CrashReport.initCrashReport(getApplicationContext(), "bd3dbcafc8", false);
        initUM();
        //第二个参数是指你应用在微信开放平台上的AppID
        mWxApi = WXAPIFactory.createWXAPI(this, Constants.WX_APP_ID, true);
        // 将该app注册到微信
        mWxApi.registerApp(Constants.WX_APP_ID);

        TXLiveBase.getInstance().setLicence(this, licenceURL, licenceKey);
        initJpush();
    }

    //极光推送SDK
    private void initJpush() {
        JPushInterface.setDebugMode(true);// 设置开启日志,发布时请关闭日志
        JPushInterface.init(this);// 初始化 JPush
        UtilsData.getPreference(this, "jpush").put("jpushid", JPushInterface.getRegistrationID(this));
        Log.e("jpush","--------------------");
        Log.e("jpush",JPushInterface.getRegistrationID(this)+"----");
    }

    /**初始化友盟*/
    private void initUM() {
        UMShareAPI.get(this);
        UMConfigure.init(this, "5ef5aff4570df36fda000013", "Umeng", UMConfigure.DEVICE_TYPE_PHONE, "");
    }

    /**
     * 判断微信是否已经安装
     * @param context
     * @return
     */
    public static boolean isWeixinAvilible(Context context) {
        final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mm")) {
                    return true;
                }
            }
        }

        return false;
    }

}
